class Solution
{
public:
    string minimumString(string a, string b, string c)
    {
        auto helper = [](string &s1, string &s2, string s3)
        {
            string result;
            for (int len = min(s1.size(), s2.size()); len >= 0; --len)
            {
                bool flag = true;
                for (int i = 0; i < len; ++i)
                {
                    if (s1[s1.size() - len + i] != s2[i])
                    {
                        flag = false;
                        break;
                    }
                }
                if (flag)
                {
                    result = s1 + s2.substr(len);
                    break;
                }
            }

            for (int len = min(result.size(), s3.size()); len >= 0; --len)
            {
                bool flag = true;
                for (int i = 0; i < len; ++i)
                {
                    if (result[result.size() - len + i] != s3[i])
                    {
                        flag = false;
                        break;
                    }
                }
                if (flag)
                {
                    result = result + s3.substr(len);
                    break;
                }
            }
            return result;
        };
        if (a.find(b) != string::npos)
        {
            b = "";
        }

        if (a.find(c) != string::npos)
        {
            c = "";
        }

        if (b.find(a) != string::npos)
        {
            a = "";
        }

        if (b.find(c) != string::npos)
        {
            c = "";
        }

        if (c.find(a) != string::npos)
        {
            a = "";
        }

        if (c.find(b) != string::npos)
        {
            b = "";
        }

        string result = helper(a, b, c);
        string tmp = helper(a, c, b);
        if (tmp.size() < result.size() || (tmp.size() == result.size() && tmp < result))
        {
            result = tmp;
        }
        tmp = helper(b, a, c);
        if (tmp.size() < result.size() || (tmp.size() == result.size() && tmp < result))
        {
            result = tmp;
        }
        tmp = helper(b, c, a);
        if (tmp.size() < result.size() || (tmp.size() == result.size() && tmp < result))
        {
            result = tmp;
        }
        tmp = helper(c, a, b);
        if (tmp.size() < result.size() || (tmp.size() == result.size() && tmp < result))
        {
            result = tmp;
        }
        tmp = helper(c, b, a);
        if (tmp.size() < result.size() || (tmp.size() == result.size() && tmp < result))
        {
            result = tmp;
        }

        return result;
    }
};